package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.OrderComment;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.service.OrderCommentService;
import com.javasm.service.impl.OrderCommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/customerOrderComment/*")
public class OrderCommentServlet extends BaseServlet {
    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1接收请求参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        Integer page = 1;
        Integer pageSize = 10;
        if(pageStr!=null&&!"".equals(pageStr)){
            page = Integer.parseInt(pageStr);
        }
        if(pageSizeStr!=null&&!"".equals(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        String orderIdStr = req.getParameter("orderId");
        Integer orderId = null;
        if(orderIdStr!=null&&!"".equals(orderIdStr)){
            orderId = Integer.parseInt(orderIdStr);
        }
        String commentDatetime = req.getParameter("commentDatetime");
        String sort =  req.getParameter("sort");

        OrderComment inputComment = new OrderComment(orderId,commentDatetime);
        OrderCommentService orderCommentService = new OrderCommentServiceImpl();
        Integer total = orderCommentService.countComments(inputComment);
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }
        List<OrderComment> orderComments = orderCommentService.listComments(inputComment, page, pageSize, sort);

        PageInfo returnPageInfo = new PageInfo(page,pageSize,total);
        //3反馈操作结果
        ReturnResult rrs = new ReturnResult();
        if(orderComments.size()>0){
            rrs.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());

            rrs.setPageInfo(returnPageInfo);
            rrs.setReturnData(orderComments);
        }else{
            rrs.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }

    public void queryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1接收请求参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        Integer page = 1;
        Integer pageSize = 10;
        if(pageStr!=null&&!"".equals(pageStr)){
            page = Integer.parseInt(pageStr);
        }
        if(pageSizeStr!=null&&!"".equals(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        String gameName = req.getParameter("gameName");
        String orderPaymentCustomerName = req.getParameter("orderPaymentCustomerName");
        String orderServeCustomerName = req.getParameter("orderServeCustomerName");

        String commentDatetime = req.getParameter("commentDatetime");
        String sort =  req.getParameter("sort");

        OrderComment inputComment = new OrderComment(commentDatetime, gameName, orderPaymentCustomerName, orderServeCustomerName);
        OrderCommentService orderCommentService = new OrderCommentServiceImpl();
        Integer total = orderCommentService.countAllComments(inputComment);
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }
        List<OrderComment> orderComments = orderCommentService.listAllComments(inputComment, page, pageSize, sort);

        PageInfo returnPageInfo = new PageInfo(page,pageSize,total);
        //3反馈操作结果
        ReturnResult rrs = new ReturnResult();
        if(orderComments.size()>0){
            rrs.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());

            rrs.setPageInfo(returnPageInfo);
            rrs.setReturnData(orderComments);
        }else{
            rrs.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }


    public void editCommentStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String commentIdStr = req.getParameter("commentId");
        Integer commentId = null;
        if(commentIdStr!=null&&!"".equals(commentIdStr)){
            commentId = Integer.parseInt(commentIdStr);
        }
        String commentStatusStr = req.getParameter("commentStatus");
        Integer commentStatus = null;
        if(commentStatusStr!=null&&!"".equals(commentStatusStr)){
            commentStatus = Integer.parseInt(commentStatusStr);
        }
        OrderComment orderComment = new OrderComment();
        orderComment.setCommentId(commentId);
        orderComment.setCommentStatus(commentStatus);
        OrderCommentService orderCommentService = new OrderCommentServiceImpl();
        Integer result = orderCommentService.editCommentStatus(orderComment);
        ReturnResult rrs = null;
        if(result>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());

        }else{
            rrs = new ReturnResult(
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg()
            );
        }
        resp.setContentType("application/json;charset=utf-8");

        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String commentIdStr = req.getParameter("commentId");
        Integer commentId = null;
        if(commentIdStr!=null&&!"".equals(commentIdStr)){
            commentId = Integer.parseInt(commentIdStr);
        }
        OrderComment orderComment = new OrderComment();
        orderComment.setCommentId(commentId);
        OrderCommentService orderCommentService = new OrderCommentServiceImpl();
        Integer result = orderCommentService.deleteComment(orderComment);
        ReturnResult rrs = null;
        if(result>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());

        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(), ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }

}
